<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use fast\Http; 
use PhpOffice\PhpSpreadsheet\Spreadsheet;

class Index extends Frontend
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';

    public function index()
    {
        return $this->view->fetch();
    }
    public function entry()
    {
        return $this->view->fetch();
    }
    public function news()
    {
        $newslist = [];
        return jsonp(['newslist' => $newslist, 'new' => count($newslist), 'url' => 'https://www.fastadmin.net?ref=news']);
    }
    public function export()
    {
        $url = "api/report/getalarmreportlist"; 
        // $token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOjUwLCJ1c2VyQ29kZSI6IjAwMDAiLCJ1c2VyTmFtZSI6ImdtYWRtaW4iLCJsb2dpbk5hbWUiOiJnbWFkbWluIiwicmVhbG5hbWUiOiJnbWFkbWkiLCJvcmdhbklkIjoxMCwib3JnYW5QYXJlbnRJZCI6MCwib3JnYW5Sb2xlTGlzdCI6IiwxMCwxMSwxNSwxNiwxNywxOCwxOSwyMCwyMSwyMiwyMywyNCwyNSwyNiwyNywyOCwyOSwiLCJzdGF0dXMiOjEsInVzZXJSb2xlTGlzdCI6IiwxMCwxMSwxNSwxNiwxNywxOCwxOSwyMCwyMSwyMiwyMywyNCwyNSwyNiwyNywyOCwiLCJpc1N1cGVyIjoxLCJpYXQiOjE1NzY4Mjg1ODcsImV4cCI6MTU3NjgzMDM4N30._QAfzRpEV6giljRQaSft-Xd43ds3xzpxQ0J5hA7FyOM";
        $token = session('token');
        $data = array();
        $data['organId'] = '1774';
        $data['hasSubOrgan'] = '';
        $data['imei'] = '13301387721';
        $data['alarmTypes'] = '29';
        $data['startTime'] = '2019-12-13 14:30';
        $data['endTime'] = '2019-12-20 14:30';
        $data['pageSize'] = '20';
        $data['currentPage'] = '1'; 
        $res = Http::sendRequest($url,$data,'GET',[],true,$token);  
        $result = $res['retobj']['rows'];
        foreach ($result as $key => $value) { 
            $result[$key]['k'] = date('md',strtotime($value['alarmTime']));
        }
        $d = $this->listTotree($result);  
        $excel =  new Spreadsheet();
        $a = 0;  
        foreach ($d as $k=>$v) { 
            $excel->setactivesheetindex($a);
            $excel->getActiveSheet()->setTitle($k);
            //Excel表格式,这里简略写了8列
            $letter = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N','O','P','Q','R');
            //表头数组
            $tableheader = array('机构名称');
            //填充表头信息
            for ($i = 0; $i < count($tableheader); $i++) {
                $excel->getActiveSheet()->setCellValue("$letter[$i]1", "$tableheader[$i]");
            }

            $kin = 2; 
            $info = $v;
            foreach ($v as $kk => $vv) {
                $excelData[] = array(
                    $vv['organName'],  
                ); 
                 /**
                 * 换行输出信息
                 */
                for ($i = 2; $i <= count($excelData) + 1; $i++) {
                    $j = 0;
                    foreach ($excelData[$i - 2] as $key => $value) {
                        $excel->getActiveSheet()->setCellValue("$letter[$j]$kin", "$value");
                        $j++;
                    }
                    $kin++;
                } 
            } 
            $a++; 
        }
     
        //创建Excel输入对象
        $write = new \PHPExcel_Writer_Excel5($excel);
        header("Pragma: public");
        header("Expires: 0");
        header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
        header("Content-Type:application/force-download");
        header("Content-Type:application/vnd.ms-execl");
        header("Content-Type:application/octet-stream");
        header("Content-Type:application/download");;
        header("Content-Disposition:attachment;filename=导出结果".date('Y-m-d').".xls");
        header("Content-Transfer-Encoding:binary");
        $write->save('php://output');

    }
    public function listTotree($list){  
        $t = array(); 
        foreach ($list as $v){
            $t[$v['k']][] = $v; 
        }   
        return $t;
    }

}
